#load packages#

library(cdlTools)
library(dplyr)
library(ebal)
library(ggeffects)
library(ggplot2)
library(ggthemes)
library(haven)
library(ipw)
library(lme4)
library(lmerTest)
library(MatchIt)
library(modelsummary)
library(readxl)
library(rstan)
library(sandwich)
library(srvyr)
library(tidyr)
library(tidyverse)

#loading data#

CCES18 <- read_dta("CCES18.dta")
CCES20 <- read_dta("CCES20.dta")
CCES22 <- read_dta("CCES22.dta")

MSA <- read_excel("MSA.xlsx")

RUCC <- read_excel("RUCC.xlsx")

#data wrangling#

#2018#

dataframe18 <- CCES18

dataframe18 <- dataframe18%>%
  mutate(attendmeetings = case_when(CC18_417a_1 == 1 ~ 1,
                                   CC18_417a_1 == 2 ~ 0),
        politicalsign = case_when(CC18_417a_2 == 1 ~ 1,
                                  CC18_417a_2 == 2 ~ 0),
        workedcampaign = case_when(CC18_417a_3 == 1 ~ 1,
                                   CC18_417a_3 == 2 ~ 0),
        attendprotest = case_when(CC18_417a_4 == 1 ~ 1,
                                  CC18_417a_4 == 2 ~ 0),
        contactofficial = case_when(CC18_417a_5 == 1 ~ 1,
                                    CC18_417a_5 == 2 ~ 0),
        donatecampaign = case_when(CC18_417a_6 == 1 ~ 1,
                                   CC18_417a_6 == 2 ~ 0),
        voted = case_when(CC18_401 == 1 ~ 0,
                          CC18_401 == 2 ~ 0,
                          CC18_401 == 3 ~ 0,
                          CC18_401 == 4 ~ 0,
                          CC18_401 == 5 ~ 1),
        registered = case_when(votereg_post == 1 ~ 1,
                               votereg_post == 2 ~ 0),
        vvoted = case_when(CL_matched == 1 ~ 0,
                           CL_matched == 2 ~ 1),
        pid7 = case_when(pid7 == 4 ~ 0,
                         pid7 == 3 ~ 1,
                         pid7 == 5 ~ 1,
                         pid7 == 2 ~ 2,
                         pid7 == 6 ~ 2,
                         pid7 == 1 ~ 3,
                         pid7 == 7 ~ 3),
        ideo7 = case_when(CC18_334A == 1 ~ 7, 
                          CC18_334A == 2 ~ 6,
                          CC18_334A == 3 ~ 5,
                          CC18_334A == 4 ~ 4,
                          CC18_334A == 5 ~ 3,
                          CC18_334A == 6 ~ 2,
                          CC18_334A == 7 ~ 1),
        newsint = case_when(newsint == 1 ~ 4,
                            newsint == 2 ~ 3,
                            newsint == 3 ~ 2,
                            newsint == 4 ~ 1),
        race = case_when(race == 1 ~ 1,
                         race == 2 ~ 2,
                         race == 3 ~ 3,
                         race == 4 ~ 4,
                         race == 5 ~ 5,
                         race == 6 ~ 6,
                         race == 7 ~ 7,
                         race == 8 ~ 7),
        race = factor(race, labels = c("White",
                                       "Black",
                                       "Hispanic",
                                       "Asian",
                                       "Native American",
                                       "Two or more races",
                                       "Other")),
        gender = factor(gender, labels = c("Male",
                                           "Female")),
        sexualitycollapsed = case_when(sexuality == 1 ~ 1,
                                       sexuality == 2 ~ 2,
                                       sexuality == 3 ~ 2,
                                       sexuality == 4 ~ 2,
                                       sexuality == 5 ~ 2),
        transgender = case_when(trans == 1 ~ 1,
                                trans == 2 ~ 2),
        LGBTQ = case_when(transgender == 2 & sexualitycollapsed == 1 ~ 0,
                          sexualitycollapsed == 2 ~ 1,
                          transgender == 1 ~ 2),
        LGBTQ = case_when(LGBTQ == 0 ~ 0,
                          LGBTQ == 1 ~ 1,
                          LGBTQ == 2 ~ 1),
        LGBTQ = factor(LGBTQ, labels = c("Cis/straight",
                                         "LGBT+")),
        LGBTQ_cat = case_when(transgender == 2 & sexualitycollapsed == 1 ~ 1,
                              transgender == 2 & sexualitycollapsed == 2 ~ 2,
                              transgender == 1 ~ 3),
        LGBTQ_cat = factor(LGBTQ_cat, labels = c("Cis/straight",
                                                 "Non-transgender LGB+",
                                                 "Transgender")),
        faminc_new = case_when(faminc_new == 1 ~ 1,
                               faminc_new == 2 ~ 2,
                               faminc_new == 3 ~ 3,
                               faminc_new == 4 ~ 4,
                               faminc_new == 5 ~ 5,
                               faminc_new == 6 ~ 6,
                               faminc_new == 7 ~ 7,
                               faminc_new == 8 ~ 8,
                               faminc_new == 9 ~ 9,
                               faminc_new == 10 ~ 10,
                               faminc_new == 11 ~ 11,
                               faminc_new == 12 ~ 12,
                               faminc_new == 13 ~ 13,
                               faminc_new == 14 ~ 14,
                               faminc_new == 15 ~ 15,
                               faminc_new == 16 ~ 16),
        pew_churatd = case_when(pew_churatd == 1 ~ 6,
                                pew_churatd == 2 ~ 5,
                                pew_churatd == 3 ~ 4,
                                pew_churatd == 4 ~ 3,
                                pew_churatd == 5 ~ 2,
                                pew_churatd == 6 ~ 1),
        urbanicity = case_when(urbancity == 1 ~ 1,
                               urbancity == 2 ~ 2,
                               urbancity == 3 ~ 3,
                               urbancity == 4 ~ 4),
        urbanicity = factor(urbanicity, labels = c("City",
                                                   "Suburb",
                                                   "Small town",
                                                   "Rural area"))
        )

dataframe18$age <- 2018 - dataframe18$birthyr

dataframe18$year <- 2018

table(dataframe18$attendmeetings)
table(dataframe18$politicalsign)
table(dataframe18$workedcampaign)
table(dataframe18$attendprotest)
table(dataframe18$contactofficial)
table(dataframe18$donatecampaign)
table(dataframe18$voted)
table(dataframe18$vvoted)
table(dataframe18$registered)
table(dataframe18$pid7)
table(dataframe18$ideo7)
table(dataframe18$newsint)
table(dataframe18$race)
table(dataframe18$age)
table(dataframe18$gender)
table(dataframe18$LGBTQ)
table(dataframe18$LGBTQ_cat)
table(dataframe18$educ)
table(dataframe18$faminc_new)
table(dataframe18$pew_churatd)
table(dataframe18$urbanicity)
table(dataframe18$countyfips)
table(dataframe18$year)
table(dataframe18$commonpostweight)

dataframe18 <- select(dataframe18,
                      caseid,
                      commonpostweight,
                      vvweight_post,
                      year,
                      inputstate_post,
                      countyfips,
                      attendmeetings,
                      politicalsign,
                      workedcampaign,
                      attendprotest,
                      contactofficial,
                      donatecampaign,
                      voted,
                      vvoted,
                      registered,
                      pid7,
                      ideo7,
                      newsint,
                      race,
                      age,
                      gender,
                      LGBTQ,
                      LGBTQ_cat,
                      educ,
                      faminc_new,
                      pew_churatd,
                      urbanicity)


#2020#

dataframe20 <- CCES20

dataframe20 <- dataframe20%>%
  mutate(attendmeetings = case_when(CC20_430a_1 == 1 ~ 1,
                                    CC20_430a_1 == 2 ~ 0),
         politicalsign = case_when(CC20_430a_2 == 1 ~ 1,
                                   CC20_430a_2 == 2 ~ 0),
         workedcampaign = case_when(CC20_430a_3 == 1 ~ 1,
                                    CC20_430a_3 == 2 ~ 0),
         attendprotest = case_when(CC20_430a_4 == 1 ~ 1,
                                   CC20_430a_4 == 2 ~ 0),
         contactofficial = case_when(CC20_430a_5 == 1 ~ 1,
                                     CC20_430a_5 == 2 ~ 0),
         donatecampaign = case_when(CC20_430a_6 == 1 ~ 1,
                                    CC20_430a_6 == 2 ~ 0),
         voted = case_when(CC20_401 == 1 ~ 0,
                           CC20_401 == 2 ~ 0,
                           CC20_401 == 3 ~ 0,
                           CC20_401 == 4 ~ 0,
                           CC20_401 == 5 ~ 1),
         registered = case_when(votereg_post == 1 ~ 1,
                                votereg_post == 2 ~ 0),
         pid7 = case_when(pid7 == 4 ~ 0,
                          pid7 == 3 ~ 1,
                          pid7 == 5 ~ 1,
                          pid7 == 2 ~ 2,
                          pid7 == 6 ~ 2,
                          pid7 == 1 ~ 3,
                          pid7 == 7 ~ 3),
         ideo7 = case_when(CC20_340a == 1 ~ 7, 
                          CC20_340a == 2 ~ 6,
                          CC20_340a == 3 ~ 5,
                          CC20_340a == 4 ~ 4,
                          CC20_340a == 5 ~ 3,
                          CC20_340a == 6 ~ 2,
                          CC20_340a == 7 ~ 1),
         newsint = case_when(newsint == 1 ~ 4,
                             newsint == 2 ~ 3,
                             newsint == 3 ~ 2,
                             newsint == 4 ~ 1),
         race = case_when(race == 1 ~ 1,
                          race == 2 ~ 2,
                          race == 3 ~ 3,
                          race == 4 ~ 4,
                          race == 5 ~ 5,
                          race == 6 ~ 6,
                          race == 7 ~ 7,
                          race == 8 ~ 7),
         race = factor(race, labels = c("White",
                                        "Black",
                                        "Hispanic",
                                        "Asian",
                                        "Native American",
                                        "Two or more races",
                                        "Other")),
         gender = factor(gender, labels = c("Male",
                                            "Female")),
         sexualitycollapsed = case_when(sexuality == 1 ~ 1,
                                        sexuality == 2 ~ 2,
                                        sexuality == 3 ~ 2,
                                        sexuality == 4 ~ 2,
                                        sexuality == 5 ~ 2),
         transgender = case_when(trans == 1 ~ 1,
                                 trans == 2 ~ 2),
         LGBTQ = case_when(transgender == 2 & sexualitycollapsed == 1 ~ 0,
                           sexualitycollapsed == 2 ~ 1,
                           transgender == 1 ~ 2),
         LGBTQ = case_when(LGBTQ == 0 ~ 0,
                           LGBTQ == 1 ~ 1,
                           LGBTQ == 2 ~ 1),
         LGBTQ = factor(LGBTQ, labels = c("Cis/straight",
                                          "LGBT+")),
         LGBTQ_cat = case_when(transgender == 2 & sexualitycollapsed == 1 ~ 1,
                               transgender == 2 & sexualitycollapsed == 2 ~ 2,
                               transgender == 1 ~ 3),
         LGBTQ_cat = factor(LGBTQ_cat, labels = c("Cis/straight",
                                                  "Non-transgender LGB+",
                                                  "Transgender")),
         faminc_new = case_when(faminc_new == 1 ~ 1,
                                faminc_new == 2 ~ 2,
                                faminc_new == 3 ~ 3,
                                faminc_new == 4 ~ 4,
                                faminc_new == 5 ~ 5,
                                faminc_new == 6 ~ 6,
                                faminc_new == 7 ~ 7,
                                faminc_new == 8 ~ 8,
                                faminc_new == 9 ~ 9,
                                faminc_new == 10 ~ 10,
                                faminc_new == 11 ~ 11,
                                faminc_new == 12 ~ 12,
                                faminc_new == 13 ~ 13,
                                faminc_new == 14 ~ 14,
                                faminc_new == 15 ~ 15,
                                faminc_new == 16 ~ 16),
         pew_churatd = case_when(pew_churatd == 1 ~ 6,
                                 pew_churatd == 2 ~ 5,
                                 pew_churatd == 3 ~ 4,
                                 pew_churatd == 4 ~ 3,
                                 pew_churatd == 5 ~ 2,
                                 pew_churatd == 6 ~ 1),
         urbanicity = case_when(urbancity == 1 ~ 1,
                                urbancity == 2 ~ 2,
                                urbancity == 3 ~ 3,
                                urbancity == 4 ~ 4),
         urbanicity = factor(urbanicity, labels = c("City",
                                                    "Suburb",
                                                    "Small town",
                                                    "Rural area"))
         )


dataframe20$age <- 2020 - dataframe20$birthyr


dataframe20$year <- 2020

table(dataframe20$attendmeetings)
table(dataframe20$politicalsign)
table(dataframe20$workedcampaign)
table(dataframe20$attendprotest)
table(dataframe20$contactofficial)
table(dataframe20$donatecampaign)
table(dataframe20$voted)
table(dataframe20$vvote)
table(dataframe20$registered)
table(dataframe20$pid7)
table(dataframe20$ideo7)
table(dataframe20$newsint)
table(dataframe20$race)
table(dataframe20$age)
table(dataframe20$gender)
table(dataframe20$LGBTQ)
table(dataframe20$LGBTQ_cat)
table(dataframe20$educ)
table(dataframe20$faminc_new)
table(dataframe20$pew_churatd)
table(dataframe20$urbanicity)
table(dataframe20$countyfips)
table(dataframe20$year)
table(dataframe20$commonpostweight)




dataframe20 <- dataframe20%>%
  mutate(vvote = case_when(CL_voter_status == 1 ~ 1,
                           is.na(CL_voter_status) ~ 0)
  )




table(dataframe20$vvote)






dataframe20 <- select(dataframe20,
                      caseid,
                      commonpostweight,
                      vvweight_post,
                      year,
                      inputstate_post,
                      countyfips,
                      attendmeetings,
                      politicalsign,
                      workedcampaign,
                      attendprotest,
                      contactofficial,
                      donatecampaign,
                      voted,
                      vvote,
                      registered,
                      pid7,
                      ideo7,
                      newsint,
                      race,
                      age,
                      gender,
                      LGBTQ,
                      LGBTQ_cat,
                      educ,
                      faminc_new,
                      pew_churatd,
                      urbanicity)



#2022#

dataframe22 <- CCES22

dataframe22 <- dataframe22%>%
  mutate(attendmeetings = case_when(CC22_430a_1 == 1 ~ 1,
                                    CC22_430a_1 == 2 ~ 0),
         politicalsign = case_when(CC22_430a_2 == 1 ~ 1,
                                   CC22_430a_2 == 2 ~ 0),
         workedcampaign = case_when(CC22_430a_3 == 1 ~ 1,
                                    CC22_430a_3 == 2 ~ 0),
         attendprotest = case_when(CC22_430a_4 == 1 ~ 1,
                                   CC22_430a_4 == 2 ~ 0),
         contactofficial = case_when(CC22_430a_5 == 1 ~ 1,
                                     CC22_430a_5 == 2 ~ 0),
         donatecampaign = case_when(CC22_430a_6 == 1 ~ 1,
                                    CC22_430a_6 == 2 ~ 0),
         voted = case_when(CC22_401 == 1 ~ 0,
                           CC22_401 == 2 ~ 0,
                           CC22_401 == 3 ~ 0,
                           CC22_401 == 4 ~ 0,
                           CC22_401 == 5 ~ 1),
         registered = case_when(votereg_post == 1 ~ 1,
                                votereg_post == 2 ~ 0),
         pid7 = case_when(pid7 == 4 ~ 0,
                          pid7 == 3 ~ 1,
                          pid7 == 5 ~ 1,
                          pid7 == 2 ~ 2,
                          pid7 == 6 ~ 2,
                          pid7 == 1 ~ 3,
                          pid7 == 7 ~ 3),
         ideo7= case_when(CC22_340a == 1 ~ 7, 
                          CC22_340a == 2 ~ 6,
                          CC22_340a == 3 ~ 5,
                          CC22_340a == 4 ~ 4,
                          CC22_340a == 5 ~ 3,
                          CC22_340a == 6 ~ 2,
                          CC22_340a == 7 ~ 1),
         newsint = case_when(newsint == 1 ~ 4,
                             newsint == 2 ~ 3,
                             newsint == 3 ~ 2,
                             newsint == 4 ~ 1),
         race = case_when(race == 1 ~ 1,
                          race == 2 ~ 2,
                          race == 3 ~ 3,
                          race == 4 ~ 4,
                          race == 5 ~ 5,
                          race == 6 ~ 6,
                          race == 7 ~ 7,
                          race == 8 ~ 7),
         race = factor(race, labels = c("White",
                                        "Black",
                                        "Hispanic",
                                        "Asian",
                                        "Native American",
                                        "Two or more races",
                                        "Other")),
         gender = case_when(gender4 == 1 ~ 1,
                            gender4 == 2 ~ 2),
         gender = factor(gender, labels = c("Male",
                                            "Female")),
         sexualitycollapsed = case_when(sexuality == 1 ~ 1,
                                        sexuality == 2 ~ 2,
                                        sexuality == 3 ~ 2,
                                        sexuality == 4 ~ 2,
                                        sexuality == 5 ~ 2),
         transgender = case_when(transgender == 1 ~ 1,
                                 transgender == 2 ~ 2),
         LGBTQ = case_when(transgender == 2 & sexualitycollapsed == 1 ~ 0,
                           sexualitycollapsed == 2 ~ 1,
                           transgender == 1 ~ 2,
                           gender4 == 3 ~ 3),
         LGBTQ = case_when(LGBTQ == 0 ~ 0,
                           LGBTQ == 1 ~ 1,
                           LGBTQ == 2 ~ 1,
                           LGBTQ == 3 ~ 1),
         LGBTQ = factor(LGBTQ, labels = c("Cis/straight",
                                          "LGBT+")),
         LGBTQ_cat = case_when(transgender == 2 & sexualitycollapsed == 1 ~ 1,
                               transgender == 2 & sexualitycollapsed == 2 ~ 2,
                               transgender == 1 ~ 3),
         LGBTQ_cat = factor(LGBTQ_cat, labels = c("Cis/straight",
                                                  "Non-transgender LGB+",
                                                  "Transgender")),
         faminc_new = case_when(faminc_new == 1 ~ 1,
                                faminc_new == 2 ~ 2,
                                faminc_new == 3 ~ 3,
                                faminc_new == 4 ~ 4,
                                faminc_new == 5 ~ 5,
                                faminc_new == 6 ~ 6,
                                faminc_new == 7 ~ 7,
                                faminc_new == 8 ~ 8,
                                faminc_new == 9 ~ 9,
                                faminc_new == 10 ~ 10,
                                faminc_new == 11 ~ 11,
                                faminc_new == 12 ~ 12,
                                faminc_new == 13 ~ 13,
                                faminc_new == 14 ~ 14,
                                faminc_new == 15 ~ 15,
                                faminc_new == 16 ~ 16),
         pew_churatd = case_when(pew_churatd == 1 ~ 6,
                                 pew_churatd == 2 ~ 5,
                                 pew_churatd == 3 ~ 4,
                                 pew_churatd == 4 ~ 3,
                                 pew_churatd == 5 ~ 2,
                                 pew_churatd == 6 ~ 1),
         urbanicity = case_when(urbancity == 1 ~ 1,
                                urbancity == 2 ~ 2,
                                urbancity == 3 ~ 3,
                                urbancity == 4 ~ 4),
         urbanicity = factor(urbanicity, labels = c("City",
                                                    "Suburb",
                                                    "Small town",
                                                    "Rural area"))
         )

dataframe22$age <- 2022 - dataframe22$birthyr

dataframe22$zipcode <- dataframe22$lookupzip_post

dataframe22$year <- 2022

table(dataframe22$attendmeetings)
table(dataframe22$politicalsign)
table(dataframe22$workedcampaign)
table(dataframe22$attendprotest)
table(dataframe22$contactofficial)
table(dataframe22$donatecampaign)
table(dataframe22$voted)
table(dataframe22$vvote)
table(dataframe22$registered)
table(dataframe22$pid7)
table(dataframe22$ideo7)
table(dataframe22$newsint)
table(dataframe22$race)
table(dataframe22$age)
table(dataframe22$gender)
table(dataframe22$LGBTQ)
table(dataframe22$LGBTQ_cat)
table(dataframe22$educ)
table(dataframe22$faminc_new)
table(dataframe22$pew_churatd)
table(dataframe22$urbancity)
table(dataframe22$countyfips)
table(dataframe22$year)
table(dataframe22$commonpostweight)




dataframe22 <- dataframe22%>%
  mutate(vvote = case_when(TS_voterstatus == 1 ~ 1,
               is.na(TS_voterstatus) ~ 0)
  )

table(dataframe22$vvote)








dataframe22 <- select(dataframe22,
                      caseid,
                      commonpostweight,
                      vvweight_post,
                      year,
                      inputstate_post,
                      countyfips,
                      attendmeetings,
                      politicalsign,
                      workedcampaign,
                      attendprotest,
                      contactofficial,
                      donatecampaign,
                      voted,
                      vvote,
                      registered,
                      pid7,
                      ideo7,
                      newsint,
                      race,
                      age,
                      gender,
                      LGBTQ,
                      LGBTQ_cat,
                      educ,
                      faminc_new,
                      pew_churatd,
                      urbanicity)


#merging CCES datasets#

dataframe <- full_join(dataframe18,
                       dataframe20)

dataframe <- full_join(dataframe,
                       dataframe22)

dataframe <- dataframe%>%
  mutate(year = as.factor(year))

#appending MSA Codes#

MSA$fipsstatecode <- str_pad(MSA$fipsstatecode, width = 2, side = "left", pad = "0")

MSA$fipscountycode <- str_pad(MSA$fipscountycode, width = 3, side = "left", pad = "0")

MSA$countyfips <- paste(MSA$fipsstatecode, 
                        MSA$fipscountycode, sep = "")

dataframe <- full_join(dataframe, 
                       MSA,
                       by = "countyfips")

dataframe <- dataframe %>% filter(!is.na(caseid))

#appending RUC codes#

dataframe <- full_join(dataframe,
                       RUCC,
                       by = "countyfips")


dataframe <- dataframe %>% filter(!is.na(caseid))


dataframe <- dataframe%>%
  mutate(metro = case_when(RUCC == 1 ~ 1,
                          RUCC == 2 ~ 1,
                          RUCC == 3 ~ 1,
                          RUCC == 4 ~ 0,
                          RUCC == 5 ~ 0,
                          RUCC == 6 ~ 0,
                          RUCC == 7 ~ 0,
                          RUCC == 8 ~ 0,
                          RUCC == 9 ~ 0),
         metro = case_when(metro == 0 ~ "Nonmetro",
                          metro == 1 ~ "Metro"),
         metro = as.factor(metro),
         metro_expanded = case_when(RUCC == 1 ~ 1,
                                    RUCC == 2 ~ 2,
                                    RUCC == 3 ~ 3,
                                    RUCC == 4 ~ 4,
                                    RUCC == 5 ~ 4,
                                    RUCC == 6 ~ 4,
                                    RUCC == 7 ~ 4,
                                    RUCC == 8 ~ 4,
                                    RUCC == 9 ~ 4),
         metro_expanded = case_when(RUCC == 1 ~ "Metro, 1 million+",
                                    RUCC == 2 ~ "Metro, 250k-1 million",
                                    RUCC == 3 ~ "Metro, <250k",
                                    RUCC == 4 ~ "Nonmetro"),
         metro_expanded = as.factor(metro_expanded),
         )



#appending NHGIS data#

dataframe <- full_join(dataframe, 
                       IPUMS,
                       by = "MSA_code")

dataframe <- dataframe %>% filter(!is.na(caseid))

dataframe$MSA_medianhhi <- log(dataframe$MSA_medianhhi)



#final wrangling#

dataframe$inputstate_post <- factor(dataframe$inputstate_post)


#analyses#


#LGBTQ status and participation#

#attending political meetings#

model1 <- lm(attendmeetings ~ LGBTQ + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               metro + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model1)


predict_attendmeetings <- ggpredict(model1, terms = c("LGBTQ"))


#putting up a political sign#

model2 <- lm(politicalsign ~ LGBTQ + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               metro + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model2)


predict_politicalsign <- ggpredict(model2, terms = c("LGBTQ"))


#working for a campaign#


model3 <- lm(workedcampaign ~ LGBTQ + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               metro + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model3)


predict_workedcampaign <- ggpredict(model3, terms = c("LGBTQ"))

#attending a protest#

model4 <- lm(attendprotest ~ LGBTQ + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               metro + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model4)


predict_attendprotest <- ggpredict(model4, terms = c("LGBTQ"))

#donated to campaign#

model5 <- lm(donatecampaign ~ LGBTQ + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               metro + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model5)


predict_donatecampaign <- ggpredict(model5, terms = c("LGBTQ"))



#contacted a public official#


model6 <- lm(contactofficial ~ LGBTQ + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               metro + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model6)


predict_contactofficial <- ggpredict(model6, terms = c("LGBTQ"))


#voted in election#


model7 <- lm(voted ~ LGBTQ + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               metro + 
               inputstate_post + 
               year,
             weights = vvweight_post,
             data = dataframe)

summary(model7)


predict_voted <- ggpredict(model7, terms = c("LGBTQ"))


#additional model distinguishing between subgroups#


model8 <- lm(voted ~ LGBTQ_cat + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               metro + 
               inputstate_post + 
               year,
             weights = vvweight_post,
             data = dataframe)

summary(model8)


predict_voted <- ggpredict(model8, terms = c("LGBTQ_cat"))

hypothesis_test(model8, terms = c("LGBTQ_cat"))



model  <- lm(voted ~ LGBTQ_cat +
                                       pid7 + 
                                       ideo7 +
                                       newsint + 
                                       race + 
                                       age + 
                                       gender + 
                                       educ + 
                                       faminc_new + 
                                       pew_churatd + 
                                       metro + 
                                       inputstate_post + 
                                       year,
                                     weights = vvweight_post,
                                     data = dataframe)



msummary(model, star=TRUE, output='latex')













#merging predictions#

predict_attendmeetings$DV <- "Attended political meeting"
predict_politicalsign$DV <- "Put up a political sign"
predict_workedcampaign$DV <- "Worked for a campaign"
predict_attendprotest$DV <- "Attended protest"
predict_contactofficial$DV <- "Contacted a public official"
predict_donatecampaign$DV <- "Donated to campaign"
predict_voted$DV <- "Voted in election"



predict_merged <- full_join(predict_attendmeetings,
                            predict_attendprotest)


predict_merged <- full_join(predict_merged,
                            predict_contactofficial)


predict_merged <- full_join(predict_merged,
                            predict_politicalsign)


predict_merged <- full_join(predict_merged,
                            predict_voted)


predict_merged <- full_join(predict_merged,
                            predict_workedcampaign)

predict_merged <- full_join(predict_merged,
                            predict_donatecampaign)


#merged plot#

ggplot(predict_merged,
       aes(x = DV,
           y = predicted,
           color = x)) + 
  geom_point(position = position_dodge(-0.5)) + 
  geom_pointrange(ymin = predict_merged$conf.low,
                  ymax = predict_merged$conf.high,
                  position = position_dodge(-0.5)) + 
  scale_x_discrete(limits = c("Voted in election",
                              "Donated to campaign",
                              "Contacted a public official",
                              "Attended protest",
                              "Worked for a campaign",
                              "Put up a political sign",
                              "Attended political meeting")) + 
  ylab("Pr") + 
  xlab("") +
  ylim(0, 1) + 
  theme_tufte() + 
  coord_flip() + 
  theme(legend.title=element_blank()) 



#LGBTQ status x RUCC interaction#

#attending political meetings#

model1 <- lm(attendmeetings ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model1)

predict_attendmeetings <- ggpredict(model1, terms = c("LGBTQ",
                                                      "metro"))



hypothesis_test(model1, terms = c("LGBTQ"))


hypothesis_test(model1, terms = c("metro"))


#putting up a political sign#

model2 <- lm(politicalsign ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model2)

predict_politicalsign <- ggpredict(model2, terms = c("LGBTQ",
                                                     "metro"))



hypothesis_test(model2, terms = c("LGBTQ"))

hypothesis_test(model2, terms = c("metro"))

#working for a campaign#

model3 <- lm(workedcampaign ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model3)

predict_workedcampaign <- ggpredict(model3, terms = c("LGBTQ",
                                                     "metro"))



hypothesis_test(model3, terms = c("LGBTQ"))

hypothesis_test(model3, terms = c("metro"))


#attending a protest#

model4 <- lm(attendprotest ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model4)

predict_attendprotest <- ggpredict(model4, terms = c("LGBTQ",
                                                     "metro"))



hypothesis_test(model4, terms = c("LGBTQ"))


hypothesis_test(model4, terms = c("metro"))




#donated to campaign#

model5 <- lm(donatecampaign ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model5)

predict_donatecampaign <- ggpredict(model5, terms = c("LGBTQ",
                                                      "metro"))





hypothesis_test(model5, terms = c("LGBTQ"))


hypothesis_test(model5, terms = c("metro"))





#contacted a public official#

model6 <- lm(contactofficial ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model6)

predict_contactofficial <- ggpredict(model6, terms = c("LGBTQ",
                                                       "metro"))





hypothesis_test(model6, terms = c("LGBTQ"))


hypothesis_test(model6, terms = c("metro"))






#voted in election#


model7 <- lm(voted ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model7)

predict_voted <- ggpredict(model7, terms = c("LGBTQ",
                                             "metro"))


hypothesis_test(model7, terms = c("LGBTQ"))


hypothesis_test(model7, terms = c("metro"))




#merging predictions#

predict_attendmeetings$DV <- "Attended political meeting"
predict_politicalsign$DV <- "Put up a political sign"
predict_workedcampaign$DV <- "Worked for a campaign"
predict_attendprotest$DV <- "Attended protest"
predict_contactofficial$DV <- "Contacted a public official"
predict_donatecampaign$DV <- "Donated to campaign"
predict_voted$DV <- "Voted in election"



predict_merged <- full_join(predict_attendmeetings,
                            predict_attendprotest)


predict_merged <- full_join(predict_merged,
                            predict_contactofficial)


predict_merged <- full_join(predict_merged,
                            predict_politicalsign)


predict_merged <- full_join(predict_merged,
                            predict_voted)


predict_merged <- full_join(predict_merged,
                            predict_workedcampaign)

predict_merged <- full_join(predict_merged,
                            predict_donatecampaign)



predict_merged$DV = factor(predict_merged$DV, 
                           levels = c("Attended political meeting",
                                      "Put up a political sign",
                                      "Worked for a campaign",
                                      "Attended protest",
                                      "Contacted a public official",
                                      "Donated to campaign",
                                      "Voted in election"))



ggplot(predict_merged,
       aes(x = group,
           y = predicted,
       color = x)) + 
  geom_point(position = position_dodge(-0.5)) + 
  geom_pointrange(ymin = predict_merged$conf.low,
                  ymax = predict_merged$conf.high) + 
  facet_wrap(~ DV,
             ncol = 1) + 
  theme_tufte() + 
  xlab("") + 
  ylab("Pr") +
  theme(legend.title=element_blank()) + 
  coord_flip() + 
  scale_x_discrete(limits = c("Nonmetro",
                              "Metro")) + 
  ylim(0, 1) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Cis/straight",
                                "LGBT+"))


ggsave("Figure 1.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)



#regression table#



models <- list()
models [['Attended political meetings']] <- lm(attendmeetings ~ LGBTQ*metro + 
                                                 pid7 + 
                                                 ideo7 +
                                                 newsint + 
                                                 race + 
                                                 age + 
                                                 gender + 
                                                 educ + 
                                                 faminc_new + 
                                                 pew_churatd + 
                                                 inputstate_post + 
                                                 year,
                                               weights = commonpostweight,
                                               data = dataframe)
models [['Put up political sign']] <- lm(politicalsign ~ LGBTQ*metro + 
                                                 pid7 + 
                                                 ideo7 +
                                                 newsint + 
                                                 race + 
                                                 age + 
                                                 gender + 
                                                 educ + 
                                                 faminc_new + 
                                                 pew_churatd + 
                                                 inputstate_post + 
                                                 year,
                                               weights = commonpostweight,
                                               data = dataframe)
models [['Worked for campaign']] <- lm(workedcampaign ~ LGBTQ*metro + 
                                                 pid7 + 
                                                 ideo7 +
                                                 newsint + 
                                                 race + 
                                                 age + 
                                                 gender + 
                                                 educ + 
                                                 faminc_new + 
                                                 pew_churatd + 
                                                 inputstate_post + 
                                                 year,
                                               weights = commonpostweight,
                                               data = dataframe)
models [['Attended protest']] <- lm(attendprotest ~ LGBTQ*metro + 
                                         pid7 + 
                                         ideo7 +
                                         newsint + 
                                         race + 
                                         age + 
                                         gender + 
                                         educ + 
                                         faminc_new + 
                                         pew_churatd + 
                                         inputstate_post + 
                                         year,
                                       weights = commonpostweight,
                                       data = dataframe)
models [['Contacted official']] <- lm(contactofficial ~ LGBTQ*metro + 
                                      pid7 + 
                                      ideo7 +
                                      newsint + 
                                      race + 
                                      age + 
                                      gender + 
                                      educ + 
                                      faminc_new + 
                                      pew_churatd + 
                                      inputstate_post + 
                                      year,
                                    weights = commonpostweight,
                                    data = dataframe)
models [['Donated to campaign']] <- lm(donatecampaign ~ LGBTQ*metro + 
                                        pid7 + 
                                        ideo7 +
                                        newsint + 
                                        race + 
                                        age + 
                                        gender + 
                                        educ + 
                                        faminc_new + 
                                        pew_churatd + 
                                        inputstate_post + 
                                        year,
                                      weights = commonpostweight,
                                      data = dataframe)
models [['Voted in election']] <- lm(voted ~ LGBTQ*metro + 
                                         pid7 + 
                                         ideo7 +
                                         newsint + 
                                         race + 
                                         age + 
                                         gender + 
                                         educ + 
                                         faminc_new + 
                                         pew_churatd + 
                                         inputstate_post + 
                                         year,
                                       weights = vvweight_post,
                                       data = dataframe)



msummary(models, star=TRUE, output='latex')



#is the urban-rural participation gap bigger for LGBT+ than cis/straight?#


contrast_attendmeetings <- test_predictions(model1, terms = c("LGBTQ",
                                                      "metro"))



contrast_politicalsign <- test_predictions(model2, terms = c("LGBTQ",
                                                      "metro"))




contrast_workedcampaign <- test_predictions(model3, terms = c("LGBTQ",
                                                      "metro"))


contrast_attendprotest <- test_predictions(model4, terms = c("LGBTQ",
                                                      "metro"))


contrast_donatecampaign <- test_predictions(model5, terms = c("LGBTQ",
                                                      "metro"))


contrast_contactofficial <- test_predictions(model6, terms = c("LGBTQ",
                                                      "metro"))


contrast_voted <- test_predictions(model7, terms = c("LGBTQ",
                                                      "metro"))






contrast_attendmeetings$DV <- "Attended political meeting"
contrast_politicalsign$DV <- "Put up a political sign"
contrast_workedcampaign$DV <- "Worked for a campaign"
contrast_attendprotest$DV <- "Attended protest"
contrast_contactofficial$DV <- "Contacted a public official"
contrast_donatecampaign$DV <- "Donated to campaign"
contrast_voted$DV <- "Voted in election"



contrast_merged <- full_join(contrast_attendmeetings,
                             contrast_attendprotest)


contrast_merged <- full_join(contrast_merged,
                             contrast_contactofficial)


contrast_merged <- full_join(contrast_merged,
                             contrast_politicalsign)


contrast_merged <- full_join(contrast_merged,
                             contrast_voted)


contrast_merged <- full_join(contrast_merged,
                             contrast_workedcampaign)

contrast_merged <- full_join(contrast_merged,
                             contrast_donatecampaign)



contrast_merged <- subset(contrast_merged,
                          metro=="Metro-Nonmetro")


contrast_mergedd <- subset(contrast_merged,
                          LGBTQ=="Cis/straight-Cis/straight")


contrast_mergeddd <- subset(contrast_merged,
                           LGBTQ=="LGBT+-LGBT+")





contrast_mergedddd <- full_join(contrast_mergedd,
                                contrast_mergeddd)




contrast_merged <- contrast_mergedddd





contrast_merged$Contrast <- contrast_merged$Contrast * -1
contrast_merged$conf.high <- contrast_merged$conf.high * -1
contrast_merged$conf.low <- contrast_merged$conf.low * -1





contrast_merged <- contrast_merged%>%
  mutate(status = case_when(LGBTQ == "Cis/straight-Cis/straight" ~ "Cis/straight",
                            LGBTQ == "LGBT+-LGBT+" ~ "LGBT+"),
         DVV = case_when(DV == "Attended political meeting" ~ "Attended political meeting",
                         DV == "Put up a political sign" ~ "Put up a political sign",
                         DV == "Worked for a campaign" ~ "Worked for a campaign*",
                         DV == "Attended protest" ~ "Attended protest*",
                         DV == "Contacted a public official" ~ "Contacted a public official*",
                         DV == "Donated to campaign" ~ "Donated to campaign*",
                         DV == "Voted in election" ~ "Voted in election"))







ggplot(contrast_merged,
       aes(x = DVV,
           y = Contrast,
           color = status)) + 
  geom_point(position = position_dodge(width = 0.4)) + 
  geom_pointrange(aes(ymin = conf.low,
                      ymax = conf.high),
                  position = position_dodge(width = 0.4)) + 
  scale_x_discrete(limits = c("Voted in election",
                              "Donated to campaign*",
                              "Contacted a public official*",
                              "Attended protest*",
                              "Worked for a campaign*",
                              "Put up a political sign",
                              "Attended political meeting")) + 
  geom_hline(yintercept = 0) + 
  ylab("Metro/nonmetro participation gap") + 
  xlab("") + 
  theme_tufte() + 
  coord_flip() +
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Cis/straight",
                                "LGBT+"))




ggsave("Figure 3.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)
















#robustness tests#

#across states#

#attending political meetings#

predict_attendmeetings <- ggpredict(model1, terms = c("LGBTQ",
                                                      "metro",
                                                      "inputstate_post"))


predict_attendmeetings$facet <- as.character(predict_attendmeetings$facet)


predict_attendmeetings$state <- fips(predict_attendmeetings$facet,
                                     to = "Name")


ggplot(predict_attendmeetings,
       aes(x = forcats::fct_rev(state),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_attendmeetings$conf.low,
                  ymax = predict_attendmeetings$conf.high) + 
  theme_tufte() + 
  coord_flip() + 
  facet_wrap(~ x) + 
  xlab("") + 
  ylab("Pr") +
  theme(legend.title=element_blank()) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) 



ggsave("statevariation_attendmeetings.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)





#putting up a political sign#


predict_politicalsign <- ggpredict(model2, terms = c("LGBTQ",
                                                     "metro",
                                                     "inputstate_post"))


predict_politicalsign$facet <- as.character(predict_politicalsign$facet)


predict_politicalsign$state <- fips(predict_politicalsign$facet,
                                     to = "Name")


ggplot(predict_politicalsign,
       aes(x = forcats::fct_rev(state),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_politicalsign$conf.low,
                  ymax = predict_politicalsign$conf.high) + 
  theme_tufte() + 
  coord_flip() + 
  facet_wrap(~ x) + 
  xlab("") + 
  ylab("Pr") +
  theme(legend.title=element_blank()) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) 



ggsave("statevariation_politicalsign.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)



#working for a campaign#
  
predict_workedcampaign <- ggpredict(model3, terms = c("LGBTQ",
                                                      "metro",
                                                      "inputstate_post"))


predict_workedcampaign$facet <- as.character(predict_workedcampaign$facet)


predict_workedcampaign$state <- fips(predict_workedcampaign$facet,
                                    to = "Name")


ggplot(predict_workedcampaign,
       aes(x = forcats::fct_rev(state),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_workedcampaign$conf.low,
                  ymax = predict_workedcampaign$conf.high) + 
  theme_tufte() + 
  coord_flip() + 
  facet_wrap(~ x) + 
  xlab("") + 
  ylab("Pr") +
  theme(legend.title=element_blank()) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) 




ggsave("statevariation_workedcampaign.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)


#attending a protest#

predict_attendprotest <- ggpredict(model4, terms = c("LGBTQ",
                                                     "metro",
                                                     "inputstate_post"))
  

predict_attendprotest$facet <- as.character(predict_attendprotest$facet)


predict_attendprotest$state <- fips(predict_attendprotest$facet,
                                     to = "Name")


ggplot(predict_attendprotest,
       aes(x = forcats::fct_rev(state),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_attendprotest$conf.low,
                  ymax = predict_attendprotest$conf.high) + 
  theme_tufte() + 
  coord_flip() + 
  facet_wrap(~ x) + 
  xlab("") + 
  ylab("Pr") +
  theme(legend.title=element_blank()) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) 



ggsave("statevariation_attendprotest.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)



#contacting an official#

predict_contactofficial <-  ggpredict(model5, terms = c("LGBTQ",
                                                        "metro",
                                                        "inputstate_post"))


predict_contactofficial$facet <- as.character(predict_contactofficial$facet)


predict_contactofficial$state <- fips(predict_contactofficial$facet,
                                    to = "Name")


ggplot(predict_contactofficial,
       aes(x = forcats::fct_rev(state),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_contactofficial$conf.low,
                  ymax = predict_contactofficial$conf.high) + 
  theme_tufte() + 
  coord_flip() + 
  facet_wrap(~ x) + 
  xlab("") + 
  ylab("Pr") +
  theme(legend.title=element_blank()) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) 



ggsave("statevariation_contactofficial.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)



#donating to a campaign#

predict_donatecampaign <- ggpredict(model6, terms = c("LGBTQ",
                                                      "metro",
                                                      "inputstate_post"))

predict_donatecampaign$facet <- as.character(predict_donatecampaign$facet)


predict_donatecampaign$state <- fips(predict_donatecampaign$facet,
                                      to = "Name")


ggplot(predict_donatecampaign,
       aes(x = forcats::fct_rev(state),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_donatecampaign$conf.low,
                  ymax = predict_donatecampaign$conf.high) + 
  theme_tufte() + 
  coord_flip() + 
  facet_wrap(~ x) + 
  xlab("") + 
  ylab("Pr") +
  theme(legend.title=element_blank()) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) 


ggsave("statevariation_donatecampaign.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)

                                                      
                                                                                     
#voted in election#                                      
                                                                                            
predict_voted <- ggpredict(model7, terms = c("LGBTQ",
                                             "metro",
                                             "inputstate_post"))


predict_voted$facet <- as.character(predict_voted$facet)


predict_voted$state <- fips(predict_voted$facet,
                                     to = "Name")


ggplot(predict_voted,
       aes(x = forcats::fct_rev(state),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_voted$conf.low,
                  ymax = predict_voted$conf.high) + 
  theme_tufte() + 
  coord_flip() + 
  facet_wrap(~ x) + 
  xlab("") + 
  ylab("Pr") +
  theme(legend.title=element_blank()) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) 



ggsave("statevariation_voted.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)






#across time#

#attending political meetings#


predict_attendmeetings <- ggpredict(model1, terms = c("LGBTQ",
                                                      "metro",
                                                      "year"))


ggplot(predict_attendmeetings,
       aes(x = forcats::fct_rev(x),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_attendmeetings$conf.low,
                  ymax = predict_attendmeetings$conf.high) + 
  facet_wrap(~ facet) + 
  theme_tufte() + 
  coord_flip() + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) +
  xlab("") + 
  ylab("Pr") + 
  theme(panel.spacing = unit(2, "lines"),
        legend.title=element_blank()) 


ggsave("wavevariation_attendmeetings.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)

#putting up a political sign#


predict_politicalsign <- ggpredict(model2, terms = c("LGBTQ",
                                                      "metro",
                                                      "year"))


ggplot(predict_politicalsign,
       aes(x = forcats::fct_rev(x),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_politicalsign$conf.low,
                  ymax = predict_politicalsign$conf.high) + 
  facet_wrap(~ facet) + 
  theme_tufte() + 
  coord_flip() + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) +
  xlab("") + 
  ylab("Pr") + 
  theme(panel.spacing = unit(2, "lines"),
        legend.title=element_blank()) 


ggsave("wavevariation_politicalsign.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)


#working for a campaign#




predict_workedcampaign <- ggpredict(model3, terms = c("LGBTQ",
                                                      "metro",
                                                      "year"))


ggplot(predict_workedcampaign,
       aes(x = forcats::fct_rev(x),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_workedcampaign$conf.low,
                  ymax = predict_workedcampaign$conf.high) + 
  facet_wrap(~ facet) + 
  theme_tufte() + 
  coord_flip() + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) +
  xlab("") + 
  ylab("Pr") + 
  theme(panel.spacing = unit(2, "lines"),
        legend.title=element_blank()) 


ggsave("wavevariation_workedcampaign.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)



#attending a protest#




predict_attendprotest <- ggpredict(model4, terms = c("LGBTQ",
                                                     "metro",
                                                     "year"))


ggplot(predict_attendprotest,
       aes(x = forcats::fct_rev(x),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_attendprotest$conf.low,
                  ymax = predict_attendprotest$conf.high) + 
  facet_wrap(~ facet) + 
  theme_tufte() + 
  coord_flip() + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) +
  xlab("") + 
  ylab("Pr") + 
  theme(panel.spacing = unit(2, "lines"),
        legend.title=element_blank()) 


ggsave("wavevariation_attendprotest.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)



#contacting an official#



predict_contactofficial <- ggpredict(model5, terms = c("LGBTQ",
                                                     "metro",
                                                     "year"))


ggplot(predict_contactofficial,
       aes(x = forcats::fct_rev(x),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_contactofficial$conf.low,
                  ymax = predict_contactofficial$conf.high) + 
  facet_wrap(~ facet) + 
  theme_tufte() + 
  coord_flip() + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) +
  xlab("") + 
  ylab("Pr") + 
  theme(panel.spacing = unit(2, "lines"),
        legend.title=element_blank()) 


ggsave("wavevariation_contactofficial.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)

#donating to a campaign#




predict_donatecampaign <- ggpredict(model6, terms = c("LGBTQ",
                                                       "metro",
                                                       "year"))


ggplot(predict_donatecampaign,
       aes(x = forcats::fct_rev(x),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_donatecampaign$conf.low,
                  ymax = predict_donatecampaign$conf.high) + 
  facet_wrap(~ facet) + 
  theme_tufte() + 
  coord_flip() + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) +
  xlab("") + 
  ylab("Pr") + 
  theme(panel.spacing = unit(2, "lines"),
        legend.title=element_blank()) 


ggsave("wavevariation_donatecampaign.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)



#voted in election#




predict_voted <- ggpredict(model7, terms = c("LGBTQ",
                                             "metro",
                                             "year"))


ggplot(predict_voted,
       aes(x = forcats::fct_rev(x),
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_voted$conf.low,
                  ymax = predict_voted$conf.high) + 
  facet_wrap(~ facet) + 
  theme_tufte() + 
  coord_flip() + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Metro",
                                "Nonmetro")) +
  xlab("") + 
  ylab("Pr") + 
  theme(panel.spacing = unit(2, "lines"),
        legend.title=element_blank()) 


ggsave("wavevariation_voted.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)



#expanded metro#


#attending political meetings#

model1 <- lm(attendmeetings ~ LGBTQ*metro_expanded + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model1)

predict_attendmeetings <- ggpredict(model1, terms = c("LGBTQ",
                                                      "metro_expanded"))


#putting up a political sign#

model2 <- lm(politicalsign ~ LGBTQ*metro_expanded + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model2)

predict_politicalsign <- ggpredict(model2, terms = c("LGBTQ",
                                                     "metro_expanded"))



#working for a campaign#

model3 <- lm(workedcampaign ~ LGBTQ*metro_expanded + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model3)

predict_workedcampaign <- ggpredict(model3, terms = c("LGBTQ",
                                                      "metro_expanded"))




#attending a protest#

model4 <- lm(attendprotest ~ LGBTQ*metro_expanded + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model4)

predict_attendprotest <- ggpredict(model4, terms = c("LGBTQ",
                                                     "metro_expanded"))

#donated to campaign#

model5 <- lm(donatecampaign ~ LGBTQ*metro_expanded + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model5)

predict_donatecampaign <- ggpredict(model5, terms = c("LGBTQ",
                                                      "metro_expanded"))



#contacted a public official#

model6 <- lm(contactofficial ~ LGBTQ*metro_expanded + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model6)

predict_contactofficial <- ggpredict(model6, terms = c("LGBTQ",
                                                       "metro_expanded"))




#voted in election#


model7 <- lm(voted ~ LGBTQ*metro_expanded + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model7)

predict_voted <- ggpredict(model7, terms = c("LGBTQ",
                                             "metro_expanded"))



#merging predictions#

predict_attendmeetings$DV <- "Attended political meeting"
predict_politicalsign$DV <- "Put up a political sign"
predict_workedcampaign$DV <- "Worked for a campaign"
predict_attendprotest$DV <- "Attended protest"
predict_contactofficial$DV <- "Contacted a public official"
predict_donatecampaign$DV <- "Donated to campaign"
predict_voted$DV <- "Voted in election"



predict_merged <- full_join(predict_attendmeetings,
                            predict_attendprotest)


predict_merged <- full_join(predict_merged,
                            predict_contactofficial)


predict_merged <- full_join(predict_merged,
                            predict_politicalsign)


predict_merged <- full_join(predict_merged,
                            predict_voted)


predict_merged <- full_join(predict_merged,
                            predict_workedcampaign)

predict_merged <- full_join(predict_merged,
                            predict_donatecampaign)



predict_merged$DV = factor(predict_merged$DV, 
                           levels = c("Attended political meeting",
                                      "Put up a political sign",
                                      "Worked for a campaign",
                                      "Attended protest",
                                      "Contacted a public official",
                                      "Donated to campaign",
                                      "Voted in election"))



ggplot(predict_merged,
       aes(x = group,
           y = predicted,
           color = x)) + 
  geom_point(position = position_dodge(-0.5)) + 
  geom_pointrange(ymin = predict_merged$conf.low,
                  ymax = predict_merged$conf.high) + 
  facet_wrap(~ DV,
             ncol = 1) + 
  theme_tufte() + 
  xlab("") + 
  ylab("Pr") +
  theme(legend.title=element_blank()) + 
  coord_flip() + 
  scale_x_discrete(limits = c("Nonmetro",
                              "Metro, <250k",
                              "Metro, 250k-1 million",
                              "Metro, 1 million+")) + 
  ylim(0, 1) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Cis/straight",
                                "LGBT+"))


ggsave("expandedmetro.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)


#participation and under resourced opportunities across geography#



dataframe <- full_join(dataframe,
                       affluent,
                       by = "MSA_code")


dataframe <- dataframe %>% filter(!is.na(caseid))


#attend political meetings#


model1 <- lmer(attendmeetings ~ LGBTQ*MSA_publicassistance + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model1)

predict_attendmeetings <- ggpredict(model1, terms = c("MSA_publicassistance",
                                                      "LGBTQ")
)




#putting up a political sign#


model2 <- lmer(politicalsign ~ LGBTQ*MSA_publicassistance + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model2)

predict_politicalsign <- ggpredict(model2, terms = c("MSA_publicassistance",
                                                     "LGBTQ")
)


#worked for a campaign#


model3 <- lmer(workedcampaign ~ LGBTQ*MSA_publicassistance + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model3)

predict_workedcampaign <- ggpredict(model3, terms = c("MSA_publicassistance",
                                                      "LGBTQ")
)


#attending a protest#


model4 <- lmer(attendprotest ~ LGBTQ*MSA_publicassistance + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model4)

predict_attendprotest <- ggpredict(model4, terms = c("MSA_publicassistance",
                                                     "LGBTQ")
)


#contacted a public official#



model5 <- lmer(contactofficial ~ LGBTQ*MSA_publicassistance + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model5)

predict_contactofficial<- ggpredict(model5, terms = c("MSA_publicassistance",
                                                      "LGBTQ")
)

#donated to a campaign#


model6 <- lmer(donatecampaign ~ LGBTQ*MSA_publicassistance + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model6)

predict_donatecampaign<- ggpredict(model6, terms = c("MSA_publicassistance",
                                                     "LGBTQ")
)

#voted in election#



model7 <- lmer(voted ~ LGBTQ*MSA_publicassistance + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model7)

predict_voted <- ggpredict(model7, terms = c("MSA_publicassistance",
                                             "LGBTQ")
)


#merging predictions#

predict_attendmeetings$DV <- "Attended political meeting"
predict_politicalsign$DV <- "Put up a political sign"
predict_workedcampaign$DV <- "Worked for a campaign"
predict_attendprotest$DV <- "Attended protest"
predict_contactofficial$DV <- "Contacted a public official"
predict_donatecampaign$DV <- "Donated to campaign"
predict_voted$DV <- "Voted in election"



predict_merged <- full_join(predict_attendmeetings,
                            predict_attendprotest)


predict_merged <- full_join(predict_merged,
                            predict_contactofficial)


predict_merged <- full_join(predict_merged,
                            predict_politicalsign)


predict_merged <- full_join(predict_merged,
                            predict_voted)


predict_merged <- full_join(predict_merged,
                            predict_workedcampaign)

predict_merged <- full_join(predict_merged,
                            predict_donatecampaign)



predict_merged$DV = factor(predict_merged$DV, 
                           levels = c("Attended political meeting",
                                      "Put up a political sign",
                                      "Worked for a campaign",
                                      "Attended protest",
                                      "Contacted a public official",
                                      "Donated to campaign",
                                      "Voted in election"))


ggplot(predict_merged,
       aes(x = x,
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_merged$conf.low,
                  ymax = predict_merged$conf.high) + 
  geom_line() +
  theme_tufte() + 
  facet_wrap(~ DV,
             strip.position = "bottom") + 
  ylab("Pr") + 
  xlab("Percent of households receiving food stamps/SNAP") +
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Cis/straight",
                                "LGBT+")) + 
  scale_x_continuous(position = "top") + 
  theme(legend.title=element_blank())
  

ggsave("snap.png",
       plot = last_plot(),
       dpi = 600,
       width = 8,
       height = 7)


#unemployment in civillian labor force#



model1 <- lmer(attendmeetings ~ LGBTQ*MSA_unemployment + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model1)

predict_attendmeetings <- ggpredict(model1, terms = c("MSA_unemployment",
                                                      "LGBTQ")
)




#putting up a political sign#


model2 <- lmer(politicalsign ~ LGBTQ*MSA_unemployment + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model2)

predict_politicalsign <- ggpredict(model2, terms = c("MSA_unemployment",
                                                     "LGBTQ")
)


#worked for a campaign#


model3 <- lmer(workedcampaign ~ LGBTQ*MSA_unemployment + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model3)

predict_workedcampaign <- ggpredict(model3, terms = c("MSA_unemployment",
                                                      "LGBTQ")
)


#attending a protest#


model4 <- lmer(attendprotest ~ LGBTQ*MSA_unemployment + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model4)

predict_attendprotest <- ggpredict(model4, terms = c("MSA_unemployment",
                                                     "LGBTQ")
)


#contacted a public official#



model5 <- lmer(contactofficial ~ LGBTQ*MSA_unemployment + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model5)

predict_contactofficial<- ggpredict(model5, terms = c("MSA_unemployment",
                                                      "LGBTQ")
)

#donated to a campaign#


model6 <- lmer(donatecampaign ~ LGBTQ*MSA_unemployment + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model6)

predict_donatecampaign<- ggpredict(model6, terms = c("MSA_unemployment",
                                                     "LGBTQ")
)

#voted in election#



model7 <- lmer(voted ~ LGBTQ*MSA_unemployment + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year + 
                 (1 + LGBTQ | MSA_name),
               data = dataframe,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model7)

predict_voted <- ggpredict(model7, terms = c("MSA_unemployment",
                                             "LGBTQ")
)


#merging predictions#

predict_attendmeetings$DV <- "Attended political meeting"
predict_politicalsign$DV <- "Put up a political sign"
predict_workedcampaign$DV <- "Worked for a campaign"
predict_attendprotest$DV <- "Attended protest"
predict_contactofficial$DV <- "Contacted a public official"
predict_donatecampaign$DV <- "Donated to campaign"
predict_voted$DV <- "Voted in election"



predict_merged <- full_join(predict_attendmeetings,
                            predict_attendprotest)


predict_merged <- full_join(predict_merged,
                            predict_contactofficial)


predict_merged <- full_join(predict_merged,
                            predict_politicalsign)


predict_merged <- full_join(predict_merged,
                            predict_voted)


predict_merged <- full_join(predict_merged,
                            predict_workedcampaign)

predict_merged <- full_join(predict_merged,
                            predict_donatecampaign)



predict_merged$DV = factor(predict_merged$DV, 
                           levels = c("Attended political meeting",
                                      "Put up a political sign",
                                      "Worked for a campaign",
                                      "Attended protest",
                                      "Contacted a public official",
                                      "Donated to campaign",
                                      "Voted in election"))


ggplot(predict_merged,
       aes(x = x,
           y = predicted,
           color = group)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_merged$conf.low,
                  ymax = predict_merged$conf.high) + 
  geom_line() +
  theme_tufte() + 
  facet_wrap(~ DV,
             strip.position = "bottom") + 
  ylab("Pr") + 
  xlab("Rate of unemployment in civilian labor force") +
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Cis/straight",
                                "LGBT+")) + 
  scale_x_continuous(position = "top") + 
  theme(legend.title=element_blank())


ggsave("unemployment.png",
       plot = last_plot(),
       dpi = 600,
       width = 8,
       height = 7)





































#contextual effects#


dataframe_LGBTQ <- subset(dataframe,
                          LGBTQ=="LGBT+")


#LGBT percent#

#attending political meetings#

model1 <- lmer(attendmeetings ~ MSA_percentLGBT + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 MSA_nhw +
                 MSA_medianhhi +
                 year + 
                 (1 | MSA_name),
               data = dataframe_LGBTQ,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model1)

predict_attendmeetings <- ggpredict(model1,
                                   terms = c("MSA_percentLGBT"))



#putting up a political sign#

model2 <- lmer(politicalsign ~ MSA_percentLGBT + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 MSA_nhw +
                 MSA_medianhhi +
                 year + 
                 (1 | MSA_name),
               data = dataframe_LGBTQ,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model2)

predict_politicalsign <- ggpredict(model2,
                                    terms = c("MSA_percentLGBT"))



#working for a campaign#

model3 <- lmer(workedcampaign ~ MSA_percentLGBT + 
                 pid7 +
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 MSA_nhw +
                 MSA_medianhhi +
                 year + 
                 (1 | MSA_name),
               data = dataframe_LGBTQ,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model3)

predict_workedcampaign <- ggpredict(model3,
                                    terms = c("MSA_percentLGBT"))


#attending a protest#

model4 <- lmer(attendprotest ~ MSA_percentLGBT + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 MSA_nhw +
                 MSA_medianhhi +
                 year + 
                 (1 | MSA_name),
               data = dataframe_LGBTQ,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model4)

predict_attendprotest <- ggpredict(model4,
                                   terms = c("MSA_percentLGBT"))


#contacted a public official#

model5 <- lmer(contactofficial ~ MSA_percentLGBT + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 MSA_nhw +
                 MSA_medianhhi +
                 year + 
                 (1 | MSA_name),
               data = dataframe_LGBTQ,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model5)

predict_contactofficial <- ggpredict(model5,
                                     terms = c("MSA_percentLGBT"))



#donated to campaign#



model6 <- lmer(donatecampaign ~ MSA_percentLGBT + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 MSA_nhw +
                 MSA_medianhhi +
                 year + 
                 (1 | MSA_name),
               data = dataframe_LGBTQ,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model6)

predict_donatecampaign <- ggpredict(model6,
                                     terms = c("MSA_percentLGBT"))



#voted in election#

model7 <- lmer(voted ~ MSA_percentLGBT + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 MSA_nhw +
                 MSA_medianhhi +
                 year + 
                 (1 | MSA_name),
               data = dataframe_LGBTQ,
               control = lmerControl(
                 optimizer ='optimx', 
                 optCtrl=list(method='nlminb')))

summary(model7)

predict_voted <- ggpredict(model7,
                                     terms = c("MSA_percentLGBT"))




#merging predictions#

predict_attendmeetings$DV <- "Attended political meeting"
predict_politicalsign$DV <- "Put up a political sign"
predict_workedcampaign$DV <- "Worked for a campaign"
predict_attendprotest$DV <- "Attended protest"
predict_contactofficial$DV <- "Contacted a public official"
predict_donatecampaign$DV <- "Donated to campaign"
predict_voted$DV <- "Voted in election"


predict_merged <- full_join(predict_attendmeetings,
                            predict_attendprotest)


predict_merged <- full_join(predict_merged,
                            predict_contactofficial)


predict_merged <- full_join(predict_merged,
                            predict_politicalsign)


predict_merged <- full_join(predict_merged,
                            predict_voted)


predict_merged <- full_join(predict_merged,
                            predict_workedcampaign)

predict_merged <- full_join(predict_merged,
                            predict_donatecampaign)



predict_merged$DV = factor(predict_merged$DV, 
                           levels = c("Attended political meeting",
                                      "Put up a political sign",
                                      "Worked for a campaign",
                                      "Attended protest",
                                      "Contacted a public official",
                                      "Donated to campaign",
                                      "Voted in election"))




ggplot(predict_merged,
       aes(x = x,
           y = predicted)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_merged$conf.low,
                  ymax = predict_merged$conf.high) + 
  geom_line() + 
  scale_x_continuous(position = "top") +
  facet_wrap(~ DV,
             nrow = 2,
             strip.position = "bottom") + 
  theme_tufte() + 
  xlab("Percent LGBT+ in metro area") + 
  ylab("Pr")


ggsave("Figure 3.png",
       plot = last_plot(),
       dpi = 600,
       width = 9,
       height = 6)







models <- list()
models [['Attended political meetings']] <- lmer(attendmeetings ~ MSA_percentLGBT + 
                                                   pid7 + 
                                                   ideo7 +
                                                   newsint + 
                                                   race + 
                                                   age + 
                                                   gender + 
                                                   educ + 
                                                   faminc_new + 
                                                   pew_churatd + 
                                                   MSA_nhw +
                                                   MSA_medianhhi +
                                                   year + 
                                                   (1 | MSA_name),
                                                 data = dataframe_LGBTQ,
                                                 control = lmerControl(
                                                   optimizer ='optimx', 
                                                   optCtrl=list(method='nlminb')))
models [['Put up political sign']] <- lmer(politicalsign ~ MSA_percentLGBT + 
                                             pid7 + 
                                             ideo7 +
                                             newsint + 
                                             race + 
                                             age + 
                                             gender + 
                                             educ + 
                                             faminc_new + 
                                             pew_churatd + 
                                             MSA_nhw +
                                             MSA_medianhhi +
                                             year + 
                                             (1 | MSA_name),
                                           data = dataframe_LGBTQ,
                                           control = lmerControl(
                                             optimizer ='optimx', 
                                             optCtrl=list(method='nlminb')))
models [['Worked for campaign']] <- lmer(workedcampaign ~ MSA_percentLGBT + 
                                           pid7 +
                                           ideo7 +
                                           newsint + 
                                           race + 
                                           age + 
                                           gender + 
                                           educ + 
                                           faminc_new + 
                                           pew_churatd + 
                                           MSA_nhw +
                                           MSA_medianhhi +
                                           year + 
                                           (1 | MSA_name),
                                         data = dataframe_LGBTQ,
                                         control = lmerControl(
                                           optimizer ='optimx', 
                                           optCtrl=list(method='nlminb')))
models [['Attended protest']] <- lmer(attendprotest ~ MSA_percentLGBT + 
                                        pid7 + 
                                        ideo7 +
                                        newsint + 
                                        race + 
                                        age + 
                                        gender + 
                                        educ + 
                                        faminc_new + 
                                        pew_churatd + 
                                        MSA_nhw +
                                        MSA_medianhhi +
                                        year + 
                                        (1 | MSA_name),
                                      data = dataframe_LGBTQ,
                                      control = lmerControl(
                                        optimizer ='optimx', 
                                        optCtrl=list(method='nlminb')))
models [['Contacted official']] <- lmer(contactofficial ~ MSA_percentLGBT + 
                                          pid7 + 
                                          ideo7 +
                                          newsint + 
                                          race + 
                                          age + 
                                          gender + 
                                          educ + 
                                          faminc_new + 
                                          pew_churatd + 
                                          MSA_nhw +
                                          MSA_medianhhi +
                                          year + 
                                          (1 | MSA_name),
                                        data = dataframe_LGBTQ,
                                        control = lmerControl(
                                          optimizer ='optimx', 
                                          optCtrl=list(method='nlminb')))
models [['Donated to campaign']] <- lmer(donatecampaign ~ MSA_percentLGBT + 
                                           pid7 + 
                                           ideo7 +
                                           newsint + 
                                           race + 
                                           age + 
                                           gender + 
                                           educ + 
                                           faminc_new + 
                                           pew_churatd + 
                                           MSA_nhw +
                                           MSA_medianhhi +
                                           year + 
                                           (1 | MSA_name),
                                         data = dataframe_LGBTQ,
                                         control = lmerControl(
                                           optimizer ='optimx', 
                                           optCtrl=list(method='nlminb')))
models [['Voted in election']] <- lmer(voted ~ MSA_percentLGBT + 
                                         pid7 + 
                                         ideo7 +
                                         newsint + 
                                         race + 
                                         age + 
                                         gender + 
                                         educ + 
                                         faminc_new + 
                                         pew_churatd + 
                                         MSA_nhw +
                                         MSA_medianhhi +
                                         year + 
                                         (1 | MSA_name),
                                       data = dataframe_LGBTQ,
                                       control = lmerControl(
                                         optimizer ='optimx', 
                                         optCtrl=list(method='nlminb')))



msummary(models, star=TRUE, output='latex')



#percent nhw#

predict_attendmeetings <- ggpredict(model1, terms = c("MSA_nhw"))
predict_politicalsign <- ggpredict(model2, terms = c("MSA_nhw"))
predict_workedcampaign <- ggpredict(model3, terms = c("MSA_nhw"))
predict_attendprotest <- ggpredict(model4, terms = c("MSA_nhw"))
predict_contactofficial <- ggpredict(model5, terms = c("MSA_nhw"))
predict_donatecampaign <- ggpredict(model6, terms = c("MSA_nhw"))
predict_voted <- ggpredict(model7, terms = c("MSA_nhw"))


predict_attendmeetings$DV <- "Attended political meeting"
predict_politicalsign$DV <- "Put up a political sign"
predict_workedcampaign$DV <- "Worked for a campaign"
predict_attendprotest$DV <- "Attended protest"
predict_contactofficial$DV <- "Contacted a public official"
predict_donatecampaign$DV <- "Donated to campaign"
predict_voted$DV <- "Voted in election"


predict_merged <- full_join(predict_attendmeetings,
                            predict_attendprotest)


predict_merged <- full_join(predict_merged,
                            predict_contactofficial)


predict_merged <- full_join(predict_merged,
                            predict_politicalsign)


predict_merged <- full_join(predict_merged,
                            predict_voted)


predict_merged <- full_join(predict_merged,
                            predict_workedcampaign)

predict_merged <- full_join(predict_merged,
                            predict_donatecampaign)



predict_merged$DV = factor(predict_merged$DV, 
                           levels = c("Attended political meeting",
                                      "Put up a political sign",
                                      "Worked for a campaign",
                                      "Attended protest",
                                      "Contacted a public official",
                                      "Donated to campaign",
                                      "Voted in election"))




ggplot(predict_merged,
       aes(x = x,
           y = predicted)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_merged$conf.low,
                  ymax = predict_merged$conf.high) + 
  geom_line() + 
  scale_x_continuous(position = "top") +
  facet_wrap(~ DV,
             nrow = 2,
             strip.position = "bottom") + 
  theme_tufte() + 
  xlab("Percent non-Hispanic white in metro area") + 
  ylab("Pr")


ggsave("percent_NHW.png",
       plot = last_plot(),
       dpi = 600,
       width = 8,
       height = 6)


#median hhi#


predict_attendmeetings <- ggpredict(model1, terms = c("MSA_medianhhi"))
predict_politicalsign <- ggpredict(model2, terms = c("MSA_medianhhi"))
predict_workedcampaign <- ggpredict(model3, terms = c("MSA_medianhhi"))
predict_attendprotest <- ggpredict(model4, terms = c("MSA_medianhhi"))
predict_contactofficial <- ggpredict(model5, terms = c("MSA_medianhhi"))
predict_donatecampaign <- ggpredict(model6, terms = c("MSA_medianhhi"))
predict_voted <- ggpredict(model7, terms = c("MSA_medianhhi"))


predict_attendmeetings$DV <- "Attended political meeting"
predict_politicalsign$DV <- "Put up a political sign"
predict_workedcampaign$DV <- "Worked for a campaign"
predict_attendprotest$DV <- "Attended protest"
predict_contactofficial$DV <- "Contacted a public official"
predict_donatecampaign$DV <- "Donated to campaign"
predict_voted$DV <- "Voted in election"


predict_merged <- full_join(predict_attendmeetings,
                            predict_attendprotest)


predict_merged <- full_join(predict_merged,
                            predict_contactofficial)


predict_merged <- full_join(predict_merged,
                            predict_politicalsign)


predict_merged <- full_join(predict_merged,
                            predict_voted)


predict_merged <- full_join(predict_merged,
                            predict_workedcampaign)

predict_merged <- full_join(predict_merged,
                            predict_donatecampaign)



predict_merged$DV = factor(predict_merged$DV, 
                           levels = c("Attended political meeting",
                                      "Put up a political sign",
                                      "Worked for a campaign",
                                      "Attended protest",
                                      "Contacted a public official",
                                      "Donated to campaign",
                                      "Voted in election"))




ggplot(predict_merged,
       aes(x = x,
           y = predicted)) + 
  geom_point() + 
  geom_pointrange(ymin = predict_merged$conf.low,
                  ymax = predict_merged$conf.high) + 
  geom_line() + 
  scale_x_continuous(position = "top") +
  facet_wrap(~ DV,
             nrow = 2,
             strip.position = "bottom") + 
  theme_tufte() + 
  xlab("Log median household income in metro area") + 
  ylab("Pr")


ggsave("median_hhi.png",
       plot = last_plot(),
       dpi = 600,
       width = 8,
       height = 6)


#identifying negligible effects#

#attending protest#

model4 <- lm(attendprotest ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model4)

predict_protest <- ggpredict(model4, terms = c("LGBTQ",
                                             "metro"))




hypothesis_test(model4, terms = c("LGBTQ"))


hypothesis_test(model4, terms = c("metro"))




#robust standard errors#

model4 <- lm(attendprotest ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model4)


predict_protest_robustSEs <- ggpredict(model4, terms = c("LGBTQ",
                                                       "metro"),
                                     vcov.fun = "vcovHC", 
                                     vcov.type = "HC3")

#logit model#


model4 <- glm(attendprotest ~ LGBTQ*metro + 
                pid7 + 
                ideo7 +
                newsint + 
                race + 
                age + 
                gender + 
                educ + 
                faminc_new + 
                pew_churatd + 
                inputstate_post + 
                year,
              weights = commonpostweight,
              data = dataframe, 
              family = binomial(link = "logit"))

summary(model4)

predict_protest_logit <- ggpredict(model4, terms = c("LGBTQ",
                                                   "metro"))


#coarsened exact matching#

dataframe_clean <- select(dataframe,
                          attendprotest,
                          LGBTQ,
                          metro,
                          pid7,
                          ideo7,
                          newsint,
                          race,
                          age,
                          gender,
                          educ,
                          faminc_new,
                          pew_churatd,
                          inputstate_post,
                          year)

dataframe_nomissing <- dataframe_clean[complete.cases(dataframe_clean), ]

dataframe_nomissing$LGBTQ_metro <- interaction(dataframe_nomissing$LGBTQ, 
                                               dataframe_nomissing$metro)


dataframe_metro <- dataframe_nomissing[dataframe_nomissing$metro == "Metro", ]
dataframe_nonmetro <- dataframe_nomissing[dataframe_nomissing$metro == "Nonmetro", ]

cem_match_metro  <- matchit(LGBTQ ~ pid7 + 
                              ideo7 +
                              newsint + 
                              race + 
                              age + 
                              gender + 
                              educ + 
                              faminc_new + 
                              pew_churatd,
                            data = dataframe_metro,
                            method = "cem") 


cem_match_nonmetro  <- matchit(LGBTQ ~ pid7 + 
                                 ideo7 +
                                 newsint + 
                                 race + 
                                 age + 
                                 gender + 
                                 educ + 
                                 faminc_new + 
                                 pew_churatd, 
                               data = dataframe_nonmetro,
                               method = "cem") 


matched_metro <- match.data(cem_match_metro)
matched_nonmetro <- match.data(cem_match_nonmetro)
matched_data <- rbind(matched_metro, matched_nonmetro)


model4 <- lm(attendprotest ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             data = matched_data,
             weights = weights)

summary(model4)

predict_protest_cem <- ggpredict(model4, terms = c("LGBTQ",
                                                 "metro"))

#Random intercept model#

model4 <- lmer(attendprotest ~ LGBTQ*metro + 
                pid7 + 
                ideo7 +
                newsint + 
                race + 
                age + 
                gender + 
                educ + 
                faminc_new + 
                pew_churatd + 
                year +
                (1 | inputstate_post), 
              weights = commonpostweight,
              data = dataframe)

summary(model4)

predict_protest_mlm <- ggpredict(model4, terms = c("LGBTQ",
                                                 "metro"))


#creating graph#

#merging#


predict_protest$spec <- "LPM, normal SEs"
predict_protest_robustSEs$spec <- "Robust SEs"
predict_protest_logit$spec <- "Logit"
predict_protest_cem$spec <- "CEM"
predict_protest_mlm$spec <- "Multilevel model with RIs"



predict_merged <- full_join(predict_protest,
                            predict_protest_robustSEs)


predict_merged <- full_join(predict_protest,
                            predict_protest_logit)


predict_merged <- full_join(predict_merged,
                            predict_protest_cem)


predict_merged <- full_join(predict_merged,
                            predict_protest_mlm)


predict_merged$DV = factor(predict_merged$spec, 
                           levels = c("LPM, normal SEs",
                                      "Robust SEs",
                                      "Logit",
                                      "CEM",
                                      "Multilevel model with RIs"))


ggplot(predict_merged,
       aes(x = group,
           y = predicted,
           color = x)) + 
  geom_point(position = position_dodge(-0.5)) + 
  geom_pointrange(ymin = predict_merged$conf.low,
                  ymax = predict_merged$conf.high) + 
  facet_wrap(~ DV,
             ncol = 1) + 
  theme_tufte() + 
  xlab("") + 
  ylab("Pr") +
  labs(title = "Attended protest") +
  theme(legend.title=element_blank(),
        plot.title = element_text(hjust = 0.5)) + 
  coord_flip() + 
  scale_x_discrete(limits = c("Nonmetro",
                              "Metro")) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Cis/straight",
                                "LGBT+"))


ggsave("Figure C.5.1.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)


#donated to campaign#


model4 <- lm(donatecampaign ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model4)

predict_donate <- ggpredict(model4, terms = c("LGBTQ",
                                               "metro"))




hypothesis_test(model4, terms = c("LGBTQ"))


hypothesis_test(model4, terms = c("metro"))




#robust standard errors#

model4 <- lm(donatecampaign ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model4)


predict_donate_robustSEs <- ggpredict(model4, terms = c("LGBTQ",
                                                         "metro"),
                                       vcov.fun = "vcovHC", 
                                       vcov.type = "HC3")

#logit model#


model4 <- glm(donatecampaign ~ LGBTQ*metro + 
                pid7 + 
                ideo7 +
                newsint + 
                race + 
                age + 
                gender + 
                educ + 
                faminc_new + 
                pew_churatd + 
                inputstate_post + 
                year,
              weights = commonpostweight,
              data = dataframe, 
              family = binomial(link = "logit"))

summary(model4)

predict_donate_logit <- ggpredict(model4, terms = c("LGBTQ",
                                                     "metro"))


#coarsened exact matching#

dataframe_clean <- select(dataframe,
                          donatecampaign,
                          LGBTQ,
                          metro,
                          pid7,
                          ideo7,
                          newsint,
                          race,
                          age,
                          gender,
                          educ,
                          faminc_new,
                          pew_churatd,
                          inputstate_post,
                          year)

dataframe_nomissing <- dataframe_clean[complete.cases(dataframe_clean), ]

dataframe_nomissing$LGBTQ_metro <- interaction(dataframe_nomissing$LGBTQ, 
                                               dataframe_nomissing$metro)


dataframe_metro <- dataframe_nomissing[dataframe_nomissing$metro == "Metro", ]
dataframe_nonmetro <- dataframe_nomissing[dataframe_nomissing$metro == "Nonmetro", ]

cem_match_metro  <- matchit(LGBTQ ~ pid7 + 
                              ideo7 +
                              newsint + 
                              race + 
                              age + 
                              gender + 
                              educ + 
                              faminc_new + 
                              pew_churatd,
                            data = dataframe_metro,
                            method = "cem") 


cem_match_nonmetro  <- matchit(LGBTQ ~ pid7 + 
                                 ideo7 +
                                 newsint + 
                                 race + 
                                 age + 
                                 gender + 
                                 educ + 
                                 faminc_new + 
                                 pew_churatd, 
                               data = dataframe_nonmetro,
                               method = "cem") 


matched_metro <- match.data(cem_match_metro)
matched_nonmetro <- match.data(cem_match_nonmetro)
matched_data <- rbind(matched_metro, matched_nonmetro)


model4 <- lm(donatecampaign ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             data = matched_data,
             weights = weights)

summary(model4)

predict_donate_cem <- ggpredict(model4, terms = c("LGBTQ",
                                                   "metro"))

#Random intercept model#

model4 <- lmer(donatecampaign ~ LGBTQ*metro + 
                 pid7 + 
                 ideo7 +
                 newsint + 
                 race + 
                 age + 
                 gender + 
                 educ + 
                 faminc_new + 
                 pew_churatd + 
                 year +
                 (1 | inputstate_post), 
               weights = commonpostweight,
               data = dataframe)

summary(model4)

predict_donate_mlm <- ggpredict(model4, terms = c("LGBTQ",
                                                   "metro"))


#creating graph#

#merging#


predict_donate$spec <- "LPM, normal SEs"
predict_donate_robustSEs$spec <- "Robust SEs"
predict_donate_logit$spec <- "Logit"
predict_donate_cem$spec <- "CEM"
predict_donate_mlm$spec <- "Multilevel model with RIs"



predict_merged <- full_join(predict_donate,
                            predict_donate_robustSEs)


predict_merged <- full_join(predict_donate,
                            predict_donate_logit)


predict_merged <- full_join(predict_merged,
                            predict_donate_cem)


predict_merged <- full_join(predict_merged,
                            predict_donate_mlm)


predict_merged$DV = factor(predict_merged$spec, 
                           levels = c("LPM, normal SEs",
                                      "Robust SEs",
                                      "Logit",
                                      "CEM",
                                      "Multilevel model with RIs"))


ggplot(predict_merged,
       aes(x = group,
           y = predicted,
           color = x)) + 
  geom_point(position = position_dodge(-0.5)) + 
  geom_pointrange(ymin = predict_merged$conf.low,
                  ymax = predict_merged$conf.high) + 
  facet_wrap(~ DV,
             ncol = 1) + 
  theme_tufte() + 
  xlab("") + 
  ylab("Pr") +
  labs(title = "Donated to campaign") +
  theme(legend.title=element_blank(),
        plot.title = element_text(hjust = 0.5)) + 
  coord_flip() + 
  scale_x_discrete(limits = c("Nonmetro",
                              "Metro")) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Cis/straight",
                                "LGBT+"))


ggsave("Figure C.5.2.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)


#voting in election#

model7 <- lm(voted ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model7)

predict_voted <- ggpredict(model7, terms = c("LGBTQ",
                                             "metro"))




hypothesis_test(model7, terms = c("LGBTQ"))


hypothesis_test(model7, terms = c("metro"))




#robust standard errors#

model7 <- lm(voted ~ LGBTQ*metro + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model7)


predict_voted_robustSEs <- ggpredict(model7, terms = c("LGBTQ",
                                                    "metro"),
                                  vcov.fun = "vcovHC", 
                                  vcov.type = "HC3")

#logit model#


model7 <- glm(voted ~ LGBTQ*metro + 
                pid7 + 
                ideo7 +
                newsint + 
                race + 
                age + 
                gender + 
                educ + 
                faminc_new + 
                pew_churatd + 
                inputstate_post + 
                year,
              weights = commonpostweight,
              data = dataframe, 
              family = binomial(link = "logit"))

summary(model7)

predict_voted_logit <- ggpredict(model7, terms = c("LGBTQ",
                                                   "metro"))


#coarsened exact matching#

dataframe_clean <- select(dataframe,
                          voted,
                          LGBTQ,
                          metro,
                          pid7,
                          ideo7,
                          newsint,
                          race,
                          age,
                          gender,
                          educ,
                          faminc_new,
                          pew_churatd,
                          inputstate_post,
                          year)

dataframe_nomissing <- dataframe_clean[complete.cases(dataframe_clean), ]

dataframe_nomissing$LGBTQ_metro <- interaction(dataframe_nomissing$LGBTQ, 
                                               dataframe_nomissing$metro)



cem_match_metro  <- matchit(LGBTQ ~ pid7 + 
                            ideo7 +
                            newsint + 
                            race + 
                            age + 
                            gender + 
                            educ + 
                            faminc_new + 
                            pew_churatd,
                     data = dataframe_metro,
                     method = "cem") 


cem_match_nonmetro  <- matchit(LGBTQ ~ pid7 + 
                                 ideo7 +
                                 newsint + 
                                 race + 
                                 age + 
                                 gender + 
                                 educ + 
                                 faminc_new + 
                                 pew_churatd, 
                            data = dataframe_nonmetro,
                            method = "cem") 


matched_metro <- match.data(cem_match_metro)
matched_nonmetro <- match.data(cem_match_nonmetro)
matched_data <- rbind(matched_metro, matched_nonmetro)


model7 <- lm(voted ~ LGBTQ*metro + 
                pid7 + 
                ideo7 +
                newsint + 
                race + 
                age + 
                gender + 
                educ + 
                faminc_new + 
                pew_churatd + 
                inputstate_post + 
                year,
                data = matched_data,
                weights = weights)

summary(model7)

predict_voted_cem <- ggpredict(model7, terms = c("LGBTQ",
                                                 "metro"))

#Random intercept model#

model <- lmer(voted ~ LGBTQ*metro + 
                pid7 + 
                ideo7 +
                newsint + 
                race + 
                age + 
                gender + 
                educ + 
                faminc_new + 
                pew_churatd + 
                year +
                (1 | inputstate_post), 
              weights = commonpostweight,
              data = dataframe)

summary(model7)

predict_voted_mlm <- ggpredict(model7, terms = c("LGBTQ",
                                                 "metro"))


#creating graph#

#merging#


predict_voted$spec <- "LPM, normal SEs"
predict_voted_robustSEs$spec <- "Robust SEs"
predict_voted_logit$spec <- "Logit"
predict_voted_cem$spec <- "CEM"
predict_voted_mlm$spec <- "Multilevel model with RIs"



predict_merged <- full_join(predict_voted,
                            predict_voted_robustSEs)


predict_merged <- full_join(predict_merged,
                            predict_voted_logit)


predict_merged <- full_join(predict_merged,
                            predict_voted_cem)


predict_merged <- full_join(predict_merged,
                            predict_voted_mlm)


predict_merged$DV = factor(predict_merged$spec, 
                           levels = c("LPM, normal SEs",
                                      "Robust SEs",
                                      "Logit",
                                      "CEM",
                                      "Multilevel model with RIs"))


ggplot(predict_merged,
      aes(x = group,
          y = predicted,
          color = x)) + 
  geom_point(position = position_dodge(-0.5)) + 
  geom_pointrange(ymin = predict_merged$conf.low,
                  ymax = predict_merged$conf.high) + 
  facet_wrap(~ DV,
             ncol = 1) + 
  theme_tufte() + 
  xlab("") + 
  ylab("Pr") +
  labs(title = "Voted in election") +
  theme(legend.title=element_blank(),
        plot.title = element_text(hjust = 0.5)) + 
  coord_flip() + 
  ylim(.8, 1) + 
  scale_x_discrete(limits = c("Nonmetro",
                              "Metro")) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Cis/straight",
                                "LGBT+"))


ggsave("Figure C.5.3.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)


#expanded metro measure#

model7 <- lm(attendprotest ~ LGBTQ*metro_expanded + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model7)

predict_protest <- ggpredict(model7, terms = c("LGBTQ",
                                             "metro_expanded"))




hypothesis_test(model7, terms = c("LGBTQ"))


hypothesis_test(model7, terms = c("metro_expanded"))




#robust standard errors#

model7 <- lm(attendprotest ~ LGBTQ*metro_expanded + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             weights = commonpostweight,
             data = dataframe)

summary(model7)


predict_protest_robustSEs <- ggpredict(model7, terms = c("LGBTQ",
                                                         "metro_expanded"),
                                     vcov.fun = "vcovHC", 
                                     vcov.type = "HC3")

#logit model#


model7 <- glm(attendprotest ~ LGBTQ*metro_expanded + 
                pid7 + 
                ideo7 +
                newsint + 
                race + 
                age + 
                gender + 
                educ + 
                faminc_new + 
                pew_churatd + 
                inputstate_post + 
                year,
              weights = commonpostweight,
              data = dataframe, 
              family = binomial(link = "logit"))

summary(model7)

predict_protest_logit <- ggpredict(model7, terms = c("LGBTQ",
                                                     "metro_expanded"))


#coarsened exact matching#

dataframe_clean <- select(dataframe,
                          attendprotest,
                          LGBTQ,
                          metro_expanded,
                          pid7,
                          ideo7,
                          newsint,
                          race,
                          age,
                          gender,
                          educ,
                          faminc_new,
                          pew_churatd,
                          inputstate_post,
                          year)

dataframe_nomissing <- dataframe_clean[complete.cases(dataframe_clean), ]

dataframe_nomissing$LGBTQ_metro <- interaction(dataframe_nomissing$LGBTQ, 
                                               dataframe_nomissing$metro_expanded)


dataframe_metro <- dataframe_nomissing[dataframe_nomissing$metro_expanded == "Metro, 1 million+", ]
dataframe_nonmetro <- dataframe_nomissing[dataframe_nomissing$metro_expanded == "Nonmetro", ]

cem_match_metro  <- matchit(LGBTQ ~ pid7 + 
                              ideo7 +
                              newsint + 
                              race + 
                              age + 
                              gender + 
                              educ + 
                              faminc_new + 
                              pew_churatd,
                            data = dataframe_metro,
                            method = "cem") 


cem_match_nonmetro  <- matchit(LGBTQ ~ pid7 + 
                                 ideo7 +
                                 newsint + 
                                 race + 
                                 age + 
                                 gender + 
                                 educ + 
                                 faminc_new + 
                                 pew_churatd, 
                               data = dataframe_nonmetro,
                               method = "cem") 


matched_metro <- match.data(cem_match_metro)
matched_nonmetro <- match.data(cem_match_nonmetro)
matched_data <- rbind(matched_metro, matched_nonmetro)

matched_data$metro_expanded <- factor(matched_data$metro_expanded)


model7 <- lm(attendprotest ~ LGBTQ*metro_expanded + 
               pid7 + 
               ideo7 +
               newsint + 
               race + 
               age + 
               gender + 
               educ + 
               faminc_new + 
               pew_churatd + 
               inputstate_post + 
               year,
             data = matched_data,
             weights = weights)

summary(model7)

predict_protest_cem <- ggpredict(model7, terms = c("LGBTQ",
                                                 "metro_expanded"))

#Random intercept model#

model7 <- lmer(attendprotest ~ LGBTQ*metro_expanded + 
                pid7 + 
                ideo7 +
                newsint + 
                race + 
                age + 
                gender + 
                educ + 
                faminc_new + 
                pew_churatd + 
                year +
                (1 | inputstate_post), 
              weights = commonpostweight,
              data = dataframe)

summary(model7)

predict_protest_mlm <- ggpredict(model7, terms = c("LGBTQ",
                                                   "metro_expanded"))


#creating graph#

#merging#


predict_protest$spec <- "LPM, normal SEs"
predict_protest_robustSEs$spec <- "Robust SEs"
predict_protest_logit$spec <- "Logit"
predict_protest_cem$spec <- "CEM"
predict_protest_mlm$spec <- "Multilevel model with RIs"



predict_merged <- full_join(predict_protest,
                            predict_protest_robustSEs)


predict_merged <- full_join(predict_merged,
                            predict_protest_logit)


predict_merged <- full_join(predict_merged,
                            predict_protest_cem)


predict_merged <- full_join(predict_merged,
                            predict_protest_mlm)


predict_merged$DV = factor(predict_merged$spec, 
                           levels = c("LPM, normal SEs",
                                      "Robust SEs",
                                      "Logit",
                                      "CEM",
                                      "Multilevel model with RIs"))


predict_merged <- predict_merged[!predict_merged$group %in% c("Metro, <250k", "Metro, 250k-1 million"), ]

ggplot(predict_merged,
       aes(x = group,
           y = predicted,
           color = x)) + 
  geom_point(position = position_dodge(-0.5)) + 
  geom_pointrange(ymin = predict_merged$conf.low,
                  ymax = predict_merged$conf.high) + 
  facet_wrap(~ DV,
             ncol = 1) + 
  theme_tufte() + 
  xlab("") + 
  ylab("Pr") +
  labs(title = "Attended protest") +
  theme(legend.title=element_blank(),
        plot.title = element_text(hjust = 0.5)) + 
  coord_flip() + 
  scale_x_discrete(limits = c("Nonmetro",
                              "Metro, 1 million+")) + 
  scale_color_manual(values=c("grey10", "grey50"),
                     limits = c("Cis/straight",
                                "LGBT+"))


ggsave("Figure C.5.4.png",
       plot = last_plot(),
       dpi = 600,
       height = 6,
       width = 8)

